package net.orange.framework.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.web.BaseController;
import net.orange.framework.logic.IDepartmentProcess;
import net.orange.framework.logic.IGroupProcess;
import net.orange.framework.model.Department;
import net.orange.framework.model.Group;

/**
 * The group managed bean using in JSF MVC modal. it is include the web page
 * content and control the action and page forward direction in the web site.
 */
@ManagedBean(name = "group")
@SessionScoped
public class GroupController extends BaseController {

	/**
	 * The group code want to search.
	 */
	private String sr_group_code;

	/**
	 * The group name want to search.
	 */
	private String sr_group_name;
	/**
	 * The groups
	 */
	Collection<SelectItem> groupList;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#getClazz()
	 */
	protected Class getClazz() {
		return Group.class;
	}

	/**
	 * @return Returns the sr_group_code.
	 */
	public String getSr_group_code() {
		return sr_group_code;
	}

	/**
	 * @return Returns the sr_group_name.
	 */
	public String getSr_group_name() {
		return sr_group_name;
	}

	/**
	 * @param sr_group_code
	 *            The sr_group_code to set.
	 */
	public void setSr_group_code(String sr_group_code) {
		this.sr_group_code = sr_group_code;
	}

	/**
	 * @param sr_group_name
	 *            The sr_group_name to set.
	 */
	public void setSr_group_name(String sr_group_name) {
		this.sr_group_name = sr_group_name;
	}

	/**
	 * @return the dept_code
	 */
	public String getDeparment_code() {
		Group group = (Group) content;
		return (group != null && group.getDepartment() != null) ? group
				.getDepartment().getDept_code() : null;
	}

	/**
	 * @param dept_code
	 *            the dept_code to set
	 */
	public void setDeparment_code(String dept_code) throws Exception {
		Group group = (Group) content;
		IDepartmentProcess process = (IDepartmentProcess) ProcessFactory
				.get(Department.class);

		if (group != null) {
			group.setDepartment((Department) process.find("dept_code",
					dept_code));
		}
	}

	/**
	 * Return the select item (groups) for selection component.
	 * 
	 * @return the collection of select item.
	 * @throws Exception
	 */
	public Collection getGroups() throws Exception {
		if (groupList != null)
			return groupList;
		IGroupProcess process = (IGroupProcess) ProcessFactory.get(Group.class);
		Collection c = process.listAll();
		groupList = new ArrayList<SelectItem>();

		if (c != null) {
			Iterator iter = c.iterator();
			while (iter.hasNext()) {
				SelectItem item = new SelectItem();
				Group g = (Group) iter.next();
				item.setLabel(g.getGroup_name());
				item.setValue(g.getPk() + "");
				groupList.add(item);
			}
		}
		return groupList;
	}
}
